#include "FREQ1Data.h"

#include <algorithm> 
#include "DataStructure/Common/Validation.h"

using namespace SG::DataStructure::Common;
using namespace SG::DataStructure::FEM;

std::vector<Real> SG::DataStructure::FEM::getfreqVector(const FREQ1DataSet& freqSet)
{
    std::vector<Real> allFreqs;
    const Real        DFREQ = 1e-5;  // 可设为参数

    // 收集所有频率点
    for (const auto& freqData : freqSet)
    {
        for (I64 i = 0; i <= freqData.m_NDF; ++i)
        {
            const Real freq = freqData.m_F1 + (freqData.m_DF * i);
            allFreqs.push_back (freq);
        }
    }

    // 排序并去除近似重复项
    std::sort (allFreqs.begin (), allFreqs.end ());

    std::vector<Real> fSet;
    for (const Real freq : allFreqs)
    {
        if (fSet.empty ())
        {
            fSet.push_back (freq);
        }
        else
        {
            Real last = fSet.back ();
            Real fmax = std::max (freq, last);
            Real fmin = std::min (freq, last);

            if (std::abs (freq - last) > DFREQ * (fmax - fmin))
            {
                fSet.push_back (freq);
            }
        }
    }

    return fSet;
}

bool SG::DataStructure::FEM::operator== (const FREQ1Data& val1, const FREQ1Data& val2)
{
    FIELD_COMPARE (m_sId)
    FIELD_COMPARE (m_F1)
    FIELD_COMPARE (m_DF)
    FIELD_COMPARE (m_NDF)
    FIELD_COMPARE (m_domainId)
    return true;
}